package com.yonyougov.cruxtestcase.testcase.organization.agency.multversion;

import com.yonyougov.cruxtestcase.component.CommonLogin;
import com.yonyougov.cruxtestcase.component.ZWCDInvoke;
import com.yonyougov.cruxtestcase.dto.BaseResponseDto;
import com.yonyougov.cruxtestcase.dto.BaseTestReqDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component("agencyCaseService")
public class LoginCaseService {
    private final ZWCDInvoke zwcdInvoke;
    private final CommonLogin commonLogin;

    public LoginCaseService(ZWCDInvoke zwcdInvoke, CommonLogin commonLogin) {
        this.zwcdInvoke = zwcdInvoke;
        this.commonLogin = commonLogin;
    }

    public BaseResponseDto testcase(BaseTestReqDto baseDto) {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, baseDto.getUser(), baseDto.getPassword());
            if (StringUtils.isNotBlank(token)) {
                baseResponseDto.setSuccess(true);
            } else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        } else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }
}
